import request from 'utils/request'
import axios from 'axios'

// 上传
export const uploadFile = (data, model) => {
  return request({
    url: '/file',
    method: 'post',
    data
  })
}

export const exportFormExcel = (data) => {
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      url: `${process.env.VUE_APP_BASE_API}/formData/export`, // 请求地址
      data, // 参数
      responseType: 'blob' // 表明返回服务器返回的数据类型
    }).then(response => {
      resolve(response.data)
      const disposition = response.headers['content-disposition']
      const blob = new Blob([response.data], {
        type: 'application/vnd.ms-excel'
      })
      let fileName = disposition.substring(disposition.indexOf('filename=') + 9, disposition.length)
      fileName = decodeURIComponent(fileName)
      if (window.navigator.msSaveOrOpenBlob) {
        // console.log(2)
        navigator.msSaveBlob(blob, fileName)
      } else {
        // console.log(3)
        var link = document.createElement('a')
        link.href = window.URL.createObjectURL(blob)
        link.download = fileName
        link.click()
        // 释放内存
        window.URL.revokeObjectURL(link.href)
      }
    },
    err => {
      reject(err)
    })
  })
}

export const getAlbumsLink = () => {
  return request({
    url: '/album/all',
    method: 'get',
    params: {
      status: 'up'
    }
  })
}

export const getVocationalLink = () => {
  return request({
    url: '/vocational',
    method: 'get',
    params: {
      status: 'up',
      page: 0,
      pageSize: 1000000
    }
  })
}

export const getNewsLink = () => {
  return request({
    url: '/information',
    method: 'get',
    params: {
      status: 'up',
      page: 0,
      pageSize: 1000000
    }
  })
}

export const getNewsLinkByType = (type) => {
  return request({
    url: '/information/queryByType',
    method: 'get',
    params: {
      status: 'up',
      page: 0,
      pageSize: 1000000,
      type: type
    }
  })
}

export const getNewsTypeLink = () => {
  return request({
    url: '/informationType/bySingle',
    method: 'get',
    params: {
      'single': false
    }
  })
}

export const getAllNewsTypeLink = () => {
  return request({
    url: '/informationType/all',
    method: 'get',
    params: {
      'status': 'up'
    }
  })
}

export const getBasicConfigLink = () => {
  return request({
    url: '/props/all',
    method: 'get',
    params: {
      status: 'up'
    }
  })
}

export const getContentConfigLink = () => {
  return request({
    url: '/basicItem/all',
    method: 'get',
    params: {
      status: 'up'
    }
  })
}

export const getNewpageLink = (data) => {
  return request({
    url: '/mall/others/all',
    method: 'get',
    params: data
  })
}
